package nsga;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import abstractGA.chrom.Chrom;
import abstractGA.popu.Population;
import abstractGA.result.ChangeElem;
import abstractGA.result.OneOpt;
import abstractGA.result.OutData;
import algorithm.AlgCtx;
import algorithm.SAElem;
import foundation.concurrency.Consummer;
import foundation.concurrency.Product;
import nsga.chromosome.NSRulSeqGAChrom;
import abstractGA.PopuData;

public class OutNSRulSeqGAPopuThread extends Consummer<PopuData> {

	AlgCtx algCtx = null;
	OutData outData = null;

	public OutNSRulSeqGAPopuThread(Product<PopuData> prod) {
		super(prod);
	}

	public OutNSRulSeqGAPopuThread(Product<PopuData> prod, AlgCtx algCtx, OutData outData) {
		super(prod);
		this.algCtx = algCtx;
		this.outData = outData;
	}

	@Override
	protected void consumeProduct(PopuData data) {// 保存到数据库
		
		int opti = 1;
		String runID = data.getRunID();
		String genID = data.getGenID();
		Population curPopu = data.getCurPopu();
		System.out.println("runID=" + runID + ",genID=" + genID + ",optID=" + opti);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		for (Chrom chrom : curPopu.getChroms()) {
			OneOpt oneOpt = new OneOpt();			
			oneOpt.setRunID(runID);
			oneOpt.setGenID(genID);
			oneOpt.setOptID(String.valueOf(opti));
			oneOpt.setRunTime(df.format(new Date()));
			oneOpt.setcode(chrom.getCodes().toString());
			oneOpt.setRestValue(chrom.getObjVals().get(0));
			oneOpt.setReltValue(chrom.getObjVals().get(1));
			oneOpt.setCostValue(chrom.getObjVals().get(2));
			// 配置变更元素
			//规则序列个体的变更元素
			ArrayList<ChangeElem> chgElems = new ArrayList<ChangeElem>();
			ArrayList<SAElem> chgedElems = ((NSRulSeqGAChrom) chrom).getTotChgElems();

			for (SAElem saElem : chgedElems) {
				ChangeElem changeElem = new ChangeElem();
				changeElem.setDoFID(saElem.getDoFID());
				changeElem.setElemName(saElem.getElemName());
				changeElem.setValue(saElem.getValue());
				chgElems.add(changeElem);
			}

			oneOpt.setChgElems(chgElems);
			outData.outDatabase(oneOpt);
			opti++;
		}
	}
}


